import groupItem from "../comm/groupItem.vue";
import footerTabbar from "../comm/footerTabbar.vue";
import { scrollMixin } from "../../../utils/mixin";
export default {
  name: "",
  //import引入的组件需要注入到对象中才能使用
  components: {
    groupItem,
    footerTabbar
  },
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      value: "",
      active: 1,
      chatStrId: "",
      isLoadMore: true,
      total_page: 0,
      page: 1,
      keyword: "",
      memberList: []
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    searchChange(value) {
      this.page = 1;
      this.keyword = value;
      if (this.timer) clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.debounce(this.getMemberList());
      }, 500);
    },
    getMemberList() {
      let query = {
        page: this.page,
        keyword: this.keyword,
        chat_str_id: this.chatStrId
      };
      if (this.fun.getKey("client") === "work" || window.localStorage.getItem("client")) {
        query = { ...this.getConfig, ...query };
      }
      $http.post("plugin.wechat-video-courses.frontend.controller.manage.record.group-member", query, "加载中..").then(response => {
        if (response.result == 1) {
          let res = response.data;
          let memberList = res.group_statistics.data;
          this.isLoadMore = true;
          this.total_page = res.group_statistics.last_page;
          if (!this.total_page) {
            this.query.page = 0;
          }
          this.memberList = memberList;
        } else {
          this.page = this.page - 1;
          this.isLoadMore = false;
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        let query = {
          page: this.page,
          keyword: this.keyword,
          chat_str_id: this.chatStrId
        };
        if (this.fun.getKey("client") === "work" || window.localStorage.getItem("client")) {
          query = { ...this.getConfig, ...query };
        }
        $http.post("plugin.wechat-video-courses.frontend.controller.manage.record.group-member", query, "加载中").then(
          res => {
            this.isLoadMore = true;
            if (res.result === 1) {
              let data = res.data;
              let memberList = data.group_statistics.data;
              this.memberList = [...this.memberList, ...memberList]; //数组拼接
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            console.log("response", response);
            // error callback
          }
        );
      }
    },
    debounce(fn, delay) {
      let last, deferTimer;
      return function(args) {
        let that = this;
        let _args = arguments;
        let now = +new Date();
        if (last && now < last + delay) {
          clearTimeout(deferTimer);
          deferTimer = setTimeout(function() {
            last = now;
            fn.apply(that, _args);
          }, delay);
        } else {
          last = now;
          fn.apply(that, _args);
        }
      };
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    if(this.fun.getKey("client") === "work" || window.localStorage.getItem("client")) {
      this.chatStrId = window.localStorage.getItem("chat_id") || '';
    }
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    this.active = 1;
    this.getMemberList();
  } //如果页面有keep-alive缓存功能，这个函数会触发
};
